package com.xuxueli.crawler.test.util;

import com.xuxueli.crawler.test.AllCategories;
import com.xuxueli.crawler.test.Allcart;
import com.xuxueli.crawler.test.entity.Cart;
import com.xuxueli.crawler.test.entity.Cartdetail;
import com.xuxueli.crawler.test.entity.Category;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class HibernateUtil {

    Configuration config = null;
    SessionFactory sessionFactory = null;
    Session session = null;
    Transaction tx = null;


    public void saveCategory(AllCategories.PageVo pageVo){
        config = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        System.err.println(pageVo.toString());

        AllCategories allCategories = new AllCategories();

        List<String> shopNameList = pageVo.getShopNameList();
        List<String> shopNameListHref = pageVo.getShopNameListHref();


       int i =0;
       for (String shopName : shopNameList){

           int maxPage = 0;
           if (!"null".equals(shopNameListHref.get(i))) {
               //maxPage = allCategories.findMaxPageNumber(shopNameListHref.get(i).replace("?ref=nav", ""));
           }
           System.err.println("正在爬"+i+"/"+shopNameList.size()+"数据");
           Category category = new Category();
           category.setId((long) i);
           category.setName(shopName);
           category.setHref(shopNameListHref.get(i).replace("?ref=nav", ""));
           category.setMaxpage((long) maxPage);
           //System.out.println("href:"+shopNameListHref.get(i).replace("?ref=nav", "") +"---------maxPage:"+maxPage);
           session.save(category);
           i++;
       }
        tx.commit();
    }

    public void saveCart(Allcart.PageVo pageVo){
        config = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        System.err.println(pageVo.toString());

        List<String> shopNameList = pageVo.getShopNameList();
        List<String> shopNameListHref = pageVo.getShopNameListHref();
        List<String> shopPriceListHref = pageVo.getShopPriceListHref();
        List<String> eanList1 = pageVo.getEanList1();
        List<String> eanList2 = pageVo.getEanList2();




        int i =0;
        for (String shopName : shopNameList){

            Cart cart = new Cart();
            Date dt= new Date();
            cart.setId(dt.getTime() + UUID.randomUUID().toString());
            cart.setCartname(shopName);
            cart.setCartHtrf(shopNameListHref.get(i));
            cart.setPrice(shopPriceListHref.get(i));
            if ("null".equals(eanList1.get(i))){
                cart.setItemean(eanList2.get(i));
            } else {
                cart.setItemean(eanList1.get(i));

            }
            session.save(cart);
            i++;
        }

        tx.commit();
    }
    int i = 0;
    public void save(List<Cartdetail> cartdetailList, int max){
        config = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();
        for (Cartdetail cartdetail : cartdetailList){
            cartdetail.setId(UUID.randomUUID().toString());
            cartdetail.setCreateime(new Timestamp(new Date().getTime()));
            cartdetail.setCount((long) max);
            session.save(cartdetail);
        }
        tx.commit();
    }


    /* 查询所有的物品类别url*/
    public List<Category> selectAllCartByCategories(){

        config = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        String hql = "from Category where maxpage != 0";

        List<Category> categoriesList = session.createQuery(hql).list();
        tx.commit();
        session.close();

        return categoriesList;
    }

    /* 查询所有的物品详细信息url*/
    public List<Cart> selectAllCartDetailByAllcart() {
        config = new Configuration().configure("/hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
        tx = session.beginTransaction();

        String hql = "from Cart where ROWNUM < 1300";

        List<Cart> cartList = session.createQuery(hql).list();
        tx.commit();
        session.close();

        return cartList;
    }
}
